package cn.manyufun.myf.common.webview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;

import cn.manyufun.myf.BuildConfig;

public class BaseWebView {
    public Activity activity;
    public WebView webView;                  //提示框布局


    public ValueCallback<Uri> mUploadMessage;
    public ValueCallback<Uri[]> uploadMessageAboveL;
    public final static int FILECHOOSER_RESULTCODE = 999;

    public JsApiCallback mCallback;
    public String userAgent;

    public BaseWebView(Activity _activity, WebView webView, JsApiCallback callback) {
        init(_activity, webView);
        this.mCallback = callback;
    }

    @SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
    private void init(Activity _activity, WebView webView) {
        Log.i("xwx", "baseWebView on Init");

        this.activity = _activity;
        this.webView = webView;
        WebSettings webSettings = webView.getSettings();

        WebServerViewClient webViewClient = new WebServerViewClient(activity, this);
        webView.setWebViewClient(webViewClient);
        webView.setWebChromeClient(new WebServerViewChromeClient(activity, this));

        PostInterceptJavascriptInterface mJSSubmitIntercept = new PostInterceptJavascriptInterface(this);
        webView.addJavascriptInterface(mJSSubmitIntercept, "api");

        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setJavaScriptEnabled(true);

        userAgent = webSettings.getUserAgentString() + " myf.app/";
        webSettings.setUserAgentString(userAgent);

        webSettings.setDomStorageEnabled(true);//开启DOM
        webSettings.setUseWideViewPort(true);// 调整到适合webview大小
        webSettings.setLoadWithOverviewMode(true);// 调整到适合webview大小
        webSettings.setBlockNetworkImage(true);
        webSettings.setAppCacheEnabled(true);//开启缓存机制
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            webSettings.setMediaPlaybackRequiresUserGesture(false);//允许后台播放声音
        }


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
            WebView.setWebContentsDebuggingEnabled(true);

    }

    public interface JsApiCallback {
        public void reply(long articleID, long replyID);

        public void favor(long articleID, long replyID);

        public void like(long articleID);

        public void openCommonList(int type, long comicsId);

        public void openComicsDetail(long comicsId);

        public void openImageDetail(String imgUrl);

    }

}
